package com.byls.controller;

import com.byls.dao.LawyerCaseMapper;
import com.byls.pojo.Lawyercase;
import com.byls.repository.LawyerCaseRepository;
import com.byls.util.PageTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/lawyercase")
@RestController
public class LawyerCaseController {
    @Autowired
    private LawyerCaseMapper lawyerCaseMapper;
    @Autowired
    private LawyerCaseRepository lawyerCaseRepository;
//    根据律师id查询其案例
    @RequestMapping("/findbyid/{id}")
    public List<Lawyercase> getCase(@PathVariable("id") Integer id){
        return lawyerCaseMapper.getLawyerCase(id);
    }
     //根据案例id查询案例
    @RequestMapping("/findcasebyid/{id}")
    public Lawyercase findCaseById(@PathVariable("id") Integer id){
        return lawyerCaseRepository.findById(id).get();
    }




    @RequestMapping("/findall/{id}")
   public Page<Lawyercase> getAllCase(@PathVariable("id") Integer id){
       Pageable pageable= PageRequest.of(0,6);
       return PageTest.listConvertToPage(lawyerCaseMapper.getLawyerCase(id),pageable);
   }

    @PostMapping("/save")//tong guo RequestBody jiang jsonduixiang zhuanwei java duixiang
    public String save(@RequestBody Lawyercase lawyercase){
        Lawyercase res = lawyerCaseRepository.save(lawyercase);
        if(res!=null){
            return "success";
        }else {
            return "error";
        }
    }
    @PutMapping("/update")
    public String update(@RequestBody Lawyercase lawyercase){
        Lawyercase res = lawyerCaseRepository.save(lawyercase);
        if(res!=null){
            return "success";
        }else {
            return "error";
        }
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        lawyerCaseRepository.deleteById(id);
    }
}
